使用遗传算法计算 f(x) = 1-x^2的最大值

遗传算法计算 f(x) = 1-x^2的最大值

x 的区间是 -1<= x <= 1

F = @(x)1-x.^2;
f = @(x)2/(2^11-1)*x-1;

s = round(rand(8,11));
s = num2str(s);
s(find(s==' '))=[];
s = reshape(s, 8, 11);
for j = 1:10000
for i = 1:2
    k = round(rand(1)*8)+1;
    if  k == 9
        k = 8;
    end
    temp = s(k, :);
    wei = round(rand(1)*11)+1;
    if wei == 12
        wei = 11;
    end
    if temp(wei) == '1'
        temp(wei) = '0';
    else
        temp(wei) = '1';
    end
    s = [s ; temp];
end

for i = 3
    k = round(rand(1, 2)*8)+1;
    a = s(k(1), :);
    b = s(k(2), :);
    wei = round(rand(1, 2)*11)+1;
    temp = a(wei : 11);
    a (wei : 11) = b(wei : 11);
    b(wei : 11) = temp;
    s = [s ; a; b];
end
[ff bin] = sort(F(f(bin2dec(s))), 'descend');
s = [s(bin(1:8), :)];
end
F(f(bin2dec(s)))


 

以下是用遗传算法解f(x)=-x^2-8x+2在区间[-2,2]内的最大值的C++代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> #include <cmath> #define POP_SIZE 20 // 群数量 #define CHROM_SIZE 10 // 染色体长度 #define MAX_GENERATION 100 // 最大迭代次数 #define CROSSOVER_PROB 0.8 // 交叉概率 #define MUTATION_PROB 0.1 // 变异概率 using namespace std; // 个体结构体 struct Individual { int chrom[CHROM_SIZE]; // 染色体 double fitness; // 适应度 }; // 初始化群 void init_population(Individual population[]) { for (int i = 0; i < POP_SIZE; i++) { for (int j = 0; j < CHROM_SIZE; j++) { population[i].chrom[j] = rand() % 2; // 随机生成0或1 } } } // 解码染色体得到x double decode_chrom(int chrom[]) { double x = 0; for (int i = 0; i < CHROM_SIZE; i++) { x += chrom[i] * pow(2, CHROM_SIZE - i - 1); } x = -2 + x * (4.0 / (pow(2, CHROM_SIZE) - 1)); // 将x映射到[-2, 2]范围内 return x; } // 计算个体适应度 double calc_fitness(int chrom[]) { double x = decode_chrom(chrom); double y = -x * x - 8 * x + 2; return y; } // 计算群中所有个体的适应度 void calc_population_fitness(Individual population[]) { for (int i = 0; i < POP_SIZE; i++) { population[i].fitness = calc_fitness(population[i].chrom); } } // 选择 void select(Individual population[]) { Individual new_population[POP_SIZE]; double total_fitness = 0; for (int i = 0; i < POP_SIZE; i++) { total_fitness += population[i].fitness; } for (int i = 0; i < POP_SIZE; i++) { double r = (double)rand() / RAND_MAX * total_fitness; double sum = 0; for (int j = 0; j < POP_SIZE; j++) { sum += population[j].fitness; if (sum >= r) { for (int k = 0; k < CHROM_SIZE; k++) { new_population[i].chrom[k] = population[j].chrom[k]; } break; } } } for (int i = 0; i < POP_SIZE; i++) { for (int j = 0; j < CHROM_SIZE; j++) { population[i].chrom[j] = new_population[i].chrom[j]; } } } // 交叉 void crossover(Individual population[]) { for (int i = 0; i < POP_SIZE; i += 2) { double r = (double)rand() / RAND_MAX; if (r < CROSSOVER_PROB) { int point = rand() % CHROM_SIZE; for (int j = point; j < CHROM_SIZE; j++) { int temp = population[i].chrom[j]; population[i].chrom[j] = population[i + 1].chrom[j]; population[i + 1].chrom[j] = temp; } } } } // 变异 void mutation(Individual population[]) { for (int i = 0; i < POP_SIZE; i++) { for (int j = 0; j < CHROM_SIZE; j++) { double r = (double)rand() / RAND_MAX; if (r < MUTATION_PROB) { population[i].chrom[j] = 1 - population[i].chrom[j]; } } } } // 找到群中适应度最高的个体 int find_best_individual(Individual population[]) { int best_index = 0; for (int i = 1; i < POP_SIZE; i++) { if (population[i].fitness > population[best_index].fitness) { best_index = i; } } return best_index; } int main() { srand((unsigned)time(NULL)); Individual population[POP_SIZE]; init_population(population); for (int i = 0; i < MAX_GENERATION; i++) { calc_population_fitness(population); int best_index = find_best_individual(population); cout << "Generation " << i << ": best fitness = " << population[best_index].fitness << endl; if (population[best_index].fitness > -0.9) { // 找到最优解 cout << "Solution found in generation " << i << endl; break; } select(population); crossover(population); mutation(population); } int best_index = find_best_individual(population); double x = decode_chrom(population[best_index].chrom); double y = population[best_index].fitness; cout << "Best solution: x = " << x << ", y = " << y << endl; return 0; } ``` 其中,init_population函数用于初始化群,calc_fitness函数用于计算个体适应度,select函数用于选择,crossover函数用于交叉,mutation函数用于变异,find_best_individual函数用于找到群中适应度最高的个体。在主函数中,先初始化群,然后进行迭代,直到找到最优解或达到最大迭代次数。最终输出找到的最优解的x和y值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值